//
//  RunLoopKeepAliveVC.m
//  ShandsMod
//
//  Created by zh_s on 3/2/22.
//  Copyright © 2022 zh_s. All rights reserved.

//runLoop保活

#import "RunLoopKeepAliveVC.h"

@interface CustomThread : NSThread

@end


@implementation CustomThread

- (void)dealloc {
    JTDLog(@"___%s___",__func__)
}

@end


@interface RunLoopKeepAliveVC ()

/**! 线程*/
@property (nonatomic, strong) CustomThread *thread;

@property (nonatomic, assign) BOOL isStoped;

@end

@implementation RunLoopKeepAliveVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navTitle = @"runloop保活流程";
    self.view.backgroundColor = [UIColor redColor];
    
    UIButton *senderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:senderBtn];
    senderBtn.frame = CGRectMake(120, 300, 120, 45);
    [senderBtn setTitleColor:RandomColor forState:UIControlStateNormal];
    [senderBtn setTitle:@"停止" forState:UIControlStateNormal];
    senderBtn.backgroundColor = MAIN_BACKCOLOR;
    [senderBtn addTarget:self action:@selector(senderClick) forControlEvents:UIControlEventTouchUpInside];
    //默认是YES高亮效果
    [senderBtn setAdjustsImageWhenHighlighted:NO];
    
    
    UIButton *senderBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:senderBtn1];
    senderBtn1.frame = CGRectMake(120, 260, 120, 45);
    [senderBtn1 setTitleColor:RandomColor forState:UIControlStateNormal];
    [senderBtn1 setTitle:@"开始" forState:UIControlStateNormal];
    senderBtn1.backgroundColor = MAIN_BACKCOLOR;
    [senderBtn1 addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    //默认是YES高亮效果
    [senderBtn1 setAdjustsImageWhenHighlighted:NO];
    
    __weak typeof(self) weakSelf = self;

    self.thread = [[CustomThread alloc] initWithBlock:^{
        
        JTDLog(@"begin-----%@",[NSThread currentThread]);
        
        [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];
 
        while(weakSelf && !weakSelf.isStoped) {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        }
//
        JTDLog(@"end-----%@",[NSThread currentThread]);
    }];
    [self.thread start];
    
}

- (void)startClick {
    if (!self.thread) {
        return;
    }
    [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:YES];
    JTDLog(@"--%s---%@---",__func__,[NSThread currentThread])
}

- (void)test {
    JTDLog(@"--%s---%@---",__func__,[NSThread currentThread])
}


- (void)senderClick {
    [self stoped];
}

- (void)stoped {
    ///在指定线程停止当前loop
    
    
    if (!self.thread) {
        return;
    }
    
    [self performSelector:@selector(stopThread) onThread:self.thread withObject:nil waitUntilDone:YES];
}

- (void)stopThread {
    self.isStoped = YES;
    CFRunLoopStop(CFRunLoopGetCurrent());
    JTDLog(@"--%s---%@---",__func__,[NSThread currentThread])
    //清空线程指针,防止子线程停止后再次调用子线程执行任务
    self.thread = nil;
}

- (void)dealloc {
    [self stoped];
    JTDLog(@"---%s---",__func__)
}

@end
